<?php
namespace app\common\library\helper;

use think\captcha\Captcha;
use think\Image;

/**
 * Class ImageHelper
 * @package app\common\lib\helper
 * @desc 图片处理
 */

class ImageHelper{

    /**
     * @desc 图片裁剪
     * @param $path
     * @param $width
     * @param $height
     * @param $position_x
     * @param $position_y
     * @return array|mixed
     */
    public static function crop($path, $width, $height, $position_x, $position_y ){

        $image = Image::open( $path );

        $image->crop( $width, $height, $position_x, $position_y )->save( $path );

        $res = [
            'filename_full' => $path,
        ];

        $res = Helper::filterSlash( $res );

        $res[ 'filename_relative' ] = str_replace( $_SERVER[ 'DOCUMENT_ROOT' ] , '', $res[ 'filename_full' ] );

        return $res;

    }

    /**
     * @param $path
     * @param $width
     * @param $height
     * @param bool $is_new      //是否生成新图片
     * @return array|mixed
     * @desc 图片等比例缩略
     */
    public static function thumb( $path, $width, $height, $is_new = false ){

        $image         = Image::open( $path );
        $filename_full = $path;

        if( $is_new ){

            $filepath_without_ext = substr( $path, 0, strrpos( $path, '.' ) );
            $file_ext             = substr( $path, strrpos( $path, '.' ) );
            $filename_full        = "{$filepath_without_ext}_{$width}_{$height}{$file_ext}";

        }

        $image->thumb( $width, $height )->save( $filename_full );

        $res = [
            'filename_full' => $filename_full
        ];

        $res = Helper::filterSlash( $res );

        $res[ 'filename_relative' ] = str_replace( $_SERVER[ 'DOCUMENT_ROOT' ] , '', $res[ 'filename_full' ] );

        return $res;

    }

    /**
     * @desc 图片打水印
     * @param $path
     * @param $logo_path        //如果是图片路径的话，就是logo，否则就是自定义文字
     * @param int $position
     * @return array
     */
    public static function water( $path, $logo_path, $position = Image::WATER_NORTHWEST ){

        /*
        //常量，标识左上角水印
                const WATER_NORTHWEST = 1;
        //常量，标识上居中水印
                const WATER_NORTH     = 2;
        //常量，标识右上角水印
                const WATER_NORTHEAST = 3;
        //常量，标识左居中水印
                const WATER_WEST      = 4;
        //常量，标识居中水印
                const WATER_CENTER    = 5;
        //常量，标识右居中水印
                const WATER_EAST      = 6;
        //常量，标识左下角水印
                const WATER_SOUTHWEST = 7;
        //常量，标识下居中水印
                const WATER_SOUTH     = 8;
        //常量，标识右下角水印
                const WATER_SOUTHEAST = 9;
        */

        $image = Image::open( $path );
        if( ! file_exists( $logo_path ) ){      //自定义文字
            $image->text( $logo_path, ROOT_PATH . '\public\static\tff\jsw.ttf', 20, '#00000000', $position )->save( $path );
        }else{  //logo
            $image->water( $logo_path, $position )->save( $path );
        }

        $res = [
            'filename_full' => $path
        ];

        $res = Helper::filterSlash( $res );

        return $res;

    }

    /**
     * @param array $config
     * @return \think\Response
     * @desc 生成验证码
     */
    public static function generateCaptcha( $config = [] ){

        if( ! $config ){

            $config = [
                'fontSize' => 30,
                'length'   => 4,
            ];

        }

        $captcha = new Captcha( $config );

        $res = $captcha->entry();

        return $res;

    }

    /**
     * @param $captcha
     * @return bool
     * @desc 验证验证码
     */
    public static function verifyCaptcha( $captcha ){

        if( captcha_check( $captcha ) ){
            return true;
        }else{
            return false;
        }

    }

}